home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 1995 #5 & #6 / Amiga Plus CD - 1995 - No. 5 and 6.iso / pd / netz / term / extras / source / term-source.lha / Colour.c < prev    next >
C/C++ Source or Header  |  1995-06-28  |  5KB  |  224 lines

  1. /*
  2. **    Colour.c
  3. **
  4. **    Colour table support code
  5. **
  6. **    Copyright © 1990-1995 by Olaf `Olsen' Barthel
  7. **        All Rights Reserved
  8. **
  9. **    :ts=4
  10. */
  11.  
  12. #include "termGlobal.h"
  13.  
  14.     /* Colour96xColourTable(const ULONG *Source96,struct ColourTable *Table,WORD NumColours):
  15.      *
  16.      *    Convert 96 bit precision colours for colour table.
  17.      */
  18.  
  19. VOID __regargs
  20. Colour96xColourTable(const ULONG *Source96,struct ColourTable *Table,WORD NumColours)
  21. {
  22.     WORD i;
  23.  
  24.     for(i = 0 ; i < NumColours ; i++)
  25.     {
  26.         Table -> Entry[i] . Red        = *Source96++;
  27.         Table -> Entry[i] . Green    = *Source96++;
  28.         Table -> Entry[i] . Blue    = *Source96++;
  29.     }
  30. }
  31.  
  32.     /* Colour12xColourTable(const UWORD *Source12,struct ColourTable *Table,WORD NumColours):
  33.      *
  34.      *    Convert 12 bit precision colours for colour table.
  35.      */
  36.  
  37. VOID __regargs
  38. Colour12xColourTable(const UWORD *Source12,struct ColourTable *Table,WORD NumColours)
  39. {
  40.     Colour12x96(Source12,(ULONG *)Table -> Entry,NumColours);
  41. }
  42.  
  43.     /* ColourTablex96(const struct ColourTable *Table,ULONG *Dest96,WORD NumColours):
  44.      *
  45.      *    Convert colour table into 96 bit precision colours.
  46.      */
  47.  
  48. VOID __regargs
  49. ColourTablex96(const struct ColourTable *Table,ULONG *Dest96,WORD NumColours)
  50. {
  51.     WORD i;
  52.  
  53.     for(i = 0 ; i < NumColours ; i++)
  54.     {
  55.         *Dest96++ = Table -> Entry[i] . Red;
  56.         *Dest96++ = Table -> Entry[i] . Green;
  57.         *Dest96++ = Table -> Entry[i] . Blue;
  58.     }
  59. }
  60.  
  61.     /* ColourTablex12(const struct ColourTable *Table,UWORD *Dest12,WORD NumColours):
  62.      *
  63.      *    Convert colour table into 12 bit precision colours.
  64.      */
  65.  
  66. VOID __regargs
  67. ColourTablex12(const struct ColourTable *Table,UWORD *Dest12,WORD NumColours)
  68. {
  69.     ULONG    r,g,b;
  70.     WORD    i;
  71.  
  72.     for(i = 0 ; i < NumColours ; i++)
  73.     {
  74.         r = Table -> Entry[i] . Red        >> 28;
  75.         g = Table -> Entry[i] . Green    >> 28;
  76.         b = Table -> Entry[i] . Blue    >> 28;
  77.  
  78.         *Dest12++ = (r << 8) | (g << 4) | b;
  79.     }
  80. }
  81.  
  82.     /* Colour12x96(const UWORD *Source12,ULONG *Dest96,WORD NumColours):
  83.      *
  84.      *    Convert 12 bit precision colours into 96 bit colours.
  85.      */
  86.  
  87. VOID __regargs
  88. Colour12x96(const UWORD *Source12,ULONG *Dest96,WORD NumColours)
  89. {
  90.     ULONG r,g,b;
  91.     UWORD v;
  92.  
  93.     do
  94.     {
  95.         v = *Source12++;
  96.  
  97.         r = (v & 0xF00) >> 8;
  98.         g = (v & 0x0F0) >> 4;
  99.         b = (v & 0x00F);
  100.  
  101.         *Dest96++ = SPREAD((r << 4) | r);
  102.         *Dest96++ = SPREAD((g << 4) | g);
  103.         *Dest96++ = SPREAD((b << 4) | b);
  104.     }
  105.     while(--NumColours);
  106. }
  107.  
  108.     /* Colour96x12(const UWORD *Source12,ULONG *Dest96,WORD NumColours):
  109.      *
  110.      *    Convert 96 bit precision colours into 12 bit colours.
  111.      */
  112.  
  113. VOID __regargs
  114. Colour96x12(const ULONG *Source96,UWORD *Dest12,WORD NumColours)
  115. {
  116.     ULONG r,g,b;
  117.  
  118.     do
  119.     {
  120.         r = (*Source96++) >> 28;
  121.         g = (*Source96++) >> 28;
  122.         b = (*Source96++) >> 28;
  123.  
  124.         *Dest12++ = (r << 8) | (g << 4) | b;
  125.     }
  126.     while(--NumColours);
  127. }
  128.  
  129.     /* DeleteColourTable(struct ColourTable *Table):
  130.      *
  131.      *    Delete a colour table.
  132.      */
  133.  
  134. VOID __regargs
  135. DeleteColourTable(struct ColourTable *Table)
  136. {
  137.     FreeVecPooled(Table);
  138. }
  139.  
  140.     /* CreateColourTable(WORD NumEntries,UWORD *Source12,ULONG *Source96):
  141.      *
  142.      *    Create a colour table and fill it with given data.
  143.      */
  144.  
  145. struct ColourTable * __regargs
  146. CreateColourTable(WORD NumEntries,const UWORD *Source12,const ULONG *Source96)
  147. {
  148.     ColourTable *Record;
  149.  
  150.     if(Record = (ColourTable *)AllocVecPooled(sizeof(ColourTable) + NumEntries * sizeof(ColourEntry),MEMF_ANY | MEMF_CLEAR))
  151.     {
  152.         Record -> NumColours = NumEntries;
  153.  
  154.         if(Source12)
  155.             Colour12x96(Source12,(ULONG *)Record -> Entry,NumEntries);
  156.  
  157.         if(Source96)
  158.         {
  159.             WORD i;
  160.  
  161.             for(i = 0 ; i < NumEntries ; i++)
  162.             {
  163.                 Record -> Entry[i] . Red    = *Source96++;
  164.                 Record -> Entry[i] . Green    = *Source96++;
  165.                 Record -> Entry[i] . Blue    = *Source96++;
  166.             }
  167.         }
  168.     }
  169.  
  170.     return(Record);
  171. }
  172.  
  173.     /* LoadColours(struct ViewPort *VPort,const struct ColourTable *Table,const UWORD *Colour12,WORD NumColours):
  174.      *
  175.      *    Load a colour table.
  176.      */
  177.  
  178. VOID __regargs
  179. LoadColourTable(struct ViewPort *VPort,const struct ColourTable *Table,const UWORD *Colour12,WORD NumColours)
  180. {
  181.     if(GfxBase -> LibNode . lib_Version >= 39 && Table)
  182.         LoadRGB32(VPort,(ULONG *)Table);
  183.     else
  184.     {
  185.         if(Colour12)
  186.             LoadRGB4(VPort,(UWORD *)Colour12,NumColours);
  187.     }
  188. }
  189.  
  190.     /* SetColour12(struct ViewPort *VPort,WORD Number,UWORD r,UWORD g,UWORD b):
  191.      *
  192.      *    Load a single colour entry in 12 bit colour precision.
  193.      */
  194.  
  195. VOID __regargs
  196. SetColour12(struct ViewPort *VPort,WORD Number,UWORD r,UWORD g,UWORD b)
  197. {
  198.     SetRGB4(VPort,Number,r,g,b);
  199. }
  200.  
  201.     /* SetColour96(struct ViewPort *VPort,ULONG Number,ULONG r,ULONG g,ULONG b):
  202.      *
  203.      *    Load a single colour entry in 96 bit colour precision.
  204.      */
  205.  
  206. VOID __regargs
  207. SetColour96(struct ViewPort *VPort,ULONG Number,ULONG r,ULONG g,ULONG b)
  208. {
  209.     if(GfxBase -> LibNode . lib_Version >= 39)
  210.     {
  211.         ULONG Table[5];
  212.  
  213.         Table[0] = (1L << 16) | Number;
  214.         Table[1] = r;
  215.         Table[2] = g;
  216.         Table[3] = b;
  217.         Table[4] = 0;
  218.  
  219.         LoadRGB32(VPort,Table);
  220.     }
  221.     else
  222.         SetRGB4(VPort,Number,r >> 28,g >> 28,b >> 28);
  223. }
  224.